//
//  NLgoinViewController.m
//  News
//
//  Created by zhangtao on 2018/11/5.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NLgoinViewController.h"
#import "NFirstLoginSetViewController.h"
#import "AppDelegate.h"
@interface NLgoinViewController ()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UIButton *remindMe;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *protocolBtn;

@end

@implementation NLgoinViewController

-(void)viewDidLayoutSubviews{
    _mainView.frame = CGRectMake(0, navHeight-64+15, ACEWidth, 500);
    _codeBtn.layer.borderColor = MAIN_COLOR.CGColor;
    _codeBtn.layer.borderWidth = 1;
    _codeBtn.layer.cornerRadius = 3;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)sendCode:(id)sender {
    NSDictionary *param = @{@"phoneNo":_phoneTF.text,@"picCode":@""};
    [[NHTTPManage shareInstance] afPOST:HTTP_GET_LOGIN_CODE andParam:param success:^(id  _Nonnull responseObject) {
        NSLog(@"..");
    } failture:^(id  _Nonnull error) {
        NSLog(@"..");
    }];
}
- (IBAction)remindMe:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (IBAction)login:(id)sender {
    
//    NSDictionary *check = @{@"phone":_phoneTF.text,@"sms":_codeTF.text};
//
//    [[NHTTPManage shareInstance]afPOST:HTTP_SMSCODE_CHECK andParam:check success:^(id  _Nonnull responseObject) {
//        NSLog(@"..");
//        [self log];
//
//    } failture:^(id  _Nonnull error) {
//        NSLog(@".");
//        [self log];
//    }];
    
    
    [self log];
    
    
   
}

-(void)log{
    NSDictionary *param = @{@"key":_phoneTF.text,@"smsCode":_codeTF.text,@"loginType":@"1"};
    [[NHTTPManage shareInstance]afPOST:HTTP_LOGIN andParam:param success:^(id  _Nonnull responseObject) {
        NSLog(@"..");
        NLoginModel *loginModel = [NLoginModel modelObjectWithDictionary:responseObject[@"Data"]];
        
        NSUserDefaults *userDetaults = [NSUserDefaults standardUserDefaults];
        [userDetaults setValue:loginModel.token forKey:@"token"];
        [userDetaults setValue:loginModel.iDProperty forKey:@"cuid"];
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:loginModel];
        [userDetaults setObject:data forKey:@"userInfo"];
        [userDetaults synchronize];
//        if ([loginModel.ID intValue] == 0) {
//            NFirstLoginSetViewController *set = [[NFirstLoginSetViewController alloc]initWithNibName:@"NFirstLoginSetViewController" bundle:nil];
//            set.phone = self.phoneTF.text;
//            set.code = self.codeTF.text;
//            [self presentViewController:set animated:YES completion:nil];
//        }else{
        [self requestInfoWithToken:loginModel.token withGUID:loginModel.iDProperty];
        [self requestSummaryWithToken:loginModel.token withGUID:loginModel.iDProperty];
//        }
        
    } failture:^(id  _Nonnull error) {
        NSLog(@".");
    }];
}

-(void)requestSummaryWithToken:(NSString *)token withGUID:(NSString *)cuid{
    NSDictionary *param = @{@"token":token,@"cuid":cuid};
    [[NHTTPManage shareInstance]afGet:HTTP_GET_SUMMARY andParam:param success:^(id  _Nonnull responseObject) {
        NSLog(@"..");
        NSummaryModel *model = [NSummaryModel shareInstance];
        [model initWithDic:responseObject[@"Data"]];
        
        if ([model.ID intValue] > 0) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [app setMainRootViewController];
        }
        
    } failture:^(id  _Nonnull error) {
        NSLog(@".");
    }];
}

-(void)requestInfoWithToken:(NSString *)token withGUID:(NSString *)cuid{
    NSDictionary *param = @{@"token":token,@"cuid":cuid};
    [[NHTTPManage shareInstance]afGet:HTTP_GET_USERSETTING andParam:param success:^(id  _Nonnull responseObject) {
        NSLog(@"..");
        NUserModel *model = [NUserModel shareInstance];
        [model initWithDic:responseObject[@"Data"]];
        
        if ([model.ID longValue] > 0) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [app setMainRootViewController];
        }else{
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [app setMainRootViewController];
        }
        
    } failture:^(id  _Nonnull error) {
        NSLog(@".");
    }];
}

- (IBAction)protocolClick:(id)sender {
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
